package com.ujs.servicevod.controller;

import com.aliyuncs.exceptions.ClientException;
import com.ujs.commonutils.constant.R;
import com.ujs.servicebase.exception.GuliException;
import com.ujs.servicevod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author zhangshihao
 * @create 2023-08-09 12:29
 */
@RestController
@RequestMapping("/eduvod/video")
@Api(tags = "阿里云视频管理")
public class VodController {
    @Autowired
    private VodService vodService;

    @PostMapping("/uploadAlyVideo")
    @ApiOperation("上传视频api")
    public R uploadAlyVideo(MultipartFile file) {
        String videoId;
        try {
            videoId = vodService.uploadVideoAly(file);
            return R.ok().data("videoId", videoId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("上传文件失败");
        }
    }

    @DeleteMapping("/deleteAlyVideo/{videoId}")
    @ApiOperation("根据videoId，删除阿里云视频")
    public R deleteAlyVideo(@PathVariable("videoId") String videoId) {
        try {
            vodService.deleteAlyVideo(videoId);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }
    }

    @ApiOperation("删除多个视频")
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        try {
            vodService.removeMoreAlyVideo(videoIdList);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }
    }

    @GetMapping("/getPlayAuth/{vid}")
    @ApiOperation("获取视频播放凭证")
    public R getPlayAuth(@PathVariable("vid") String vid) {
        String playAuth = null;
        try {
            playAuth = vodService.getPlayAuth(vid);
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
        return R.ok().data("playAuth", playAuth);
    }
}
